package com.charles.seckillcommon.security.utils.auth;

import com.charles.seckillapi.model.UserLogin;
import com.charles.seckillcommon.core.exceptions.GlobalException;
import com.charles.seckillcommon.core.utils.SpringUtils;
import com.charles.seckillcommon.security.service.TokenService;
import com.charles.seckillcommon.security.utils.ThreadUtil;

/**
 * @author Charles-H
 * 
 * 权限验证逻辑层
 */
public class AuthVerify {
    
    public TokenService tokenService = SpringUtils.getBean(TokenService.class);

    /**
     * 注销会话
     */
    public void logOut() {
        String token = ThreadUtil.getToken();
        if (token == null) return;
        logOutByToken(token);
    }
    
    /**
     * 通过token注销会话
     */
    public void logOutByToken(String token) {
        tokenService.deleteUserLogin(token);
    }
    
    /**
     * 判断用户是否登录
     */
    public void isLogin() {
        getUserLogin();
    }

    /**
     * 获取当前客户
     */
    public UserLogin getUserLogin() {
        String token = ThreadUtil.getToken();
        if (token == null) throw new GlobalException("未登录");
        UserLogin userLogin = ThreadUtil.getUserLogin();
        if (userLogin == null) throw new GlobalException("令牌无效");
        return userLogin;
    }
    
    /**
     * 通过令牌获取客户
     */
    public UserLogin getUserLogin(String token) {
        return tokenService.getUserLogin(token);
    }
    
    /**
     * 查看当前客户有效期
     */
    public void checkUserLogin(UserLogin userLogin) {
        tokenService.refreshToken(userLogin);
    }

}
